import random
import matplotlib.pyplot as plt
# 配置中文字体
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['SimHei'] # 选择合适的中午字体
def roll_dice(num_rolls):
    results = {}


    for _  in range(num_rolls):
        roll = random.randint(1,6) # 生成1到6的随机数

        if roll in results:
            results[roll] += 1
        else:
            results[roll] = 1

    return results

num_rolls = 100
results = roll_dice(num_rolls)

# 提取骰子的点数和频率
dice_values = list(results.keys())
frequencies = list(results.values())

# 绘制柱状图
plt.bar(dice_values,frequencies)
plt.xlabel('骰子点数')
plt.ylabel('频率')
plt.title('投掷骰子100次结果')
plt.show()